import Mock from 'mockjs';

Mock.setup({
    timeout: '500-800',
});

// 侧边栏动态数据数组
const menuData = [
    {
        key: '0',
        id: '1',
        name: '商品',
        level: '一级',
        nameshow: 'pms',
        showicon: '0',
        ifshow: true,
        sort: '0',
    },
    {
        key: '1',
        id: '7',
        name: '订单',
        level: '一级',
        nameshow: 'oms',
        showicon: '0',
        ifshow: true,
        sort: '0',
    },
    {
        key: '2',
        id: '12',
        name: '营销',
        level: '一级',
        nameshow: 'sms',
        showicon: '0',
        ifshow: true,
        sort: '0',
    },
    {
        key: '3',
        id: '21',
        name: '权限',
        level: '一级',
        nameshow: 'ums',
        showicon: '0',
        ifshow: true,
        sort: '0',
    }
];

export const expandedRowData = {
    '0': [
        {
            key: '0-0',
            id: '2',
            name: '商品列表',
            level: '二级',
            nameshow: '/product',
            showicon: '0',
            ifshow: false,
            sort: '0',
        },
        {
            key: '0-1',
            id: '3',
            name: '添加商品',
            level: '二级',
            nameshow: '/addProduct',
            showicon: '0',
            ifshow: true,
            sort: '0',
        },
        {
            key: '0-2',
            id: '4',
            name: '商品分类',
            level: '二级',
            nameshow: '/productCate',
            showicon: '0',
            ifshow: true,
            sort: '0',
        },
        {
            key: '0-3',
            id: '5',
            name: '商品类型',
            level: '二级',
            nameshow: '/productAttr',
            showicon: '0',
            ifshow: true,
            sort: '0',
        },
        {
            key: '0-4',
            id: '6',
            name: '品牌管理',
            level: '二级',
            nameshow: '/brand',
            showicon: '0',
            ifshow: true,
            sort: '0',
        },
    ],
    '1': [
        {
            key: '1-0',
            id: '8',
            name: '订单列表',
            level: '二级',
            nameshow: '/order',
            showicon: '0',
            ifshow: true,
            sort: '0',
        },
        {
            key: '1-1',
            id: '9',
            name: '订单设置',
            level: '二级',
            nameshow: '/orderSetting',
            showicon: '0',
            ifshow: true,
            sort: '0',
        },
        {
            key: '1-2',
            id: '10',
            name: '退货申请处理',
            level: '二级',
            nameshow: '/returnApply',
            showicon: '0',
            ifshow: true,
            sort: '0',
        },
        {
            key: '1-3',
            id: '11',
            name: '退货原因设置',
            level: '二级',
            nameshow: '/returnReason',
            showicon: '0',
            ifshow: true,
            sort: '0',
        }
    ],
    '2': [
        {
            key: '2-0',
            id: '13',
            name: '秒杀活动列表',
            level: '二级',
            nameshow: '/flash',
            showicon: '0',
            ifshow: true,
            sort: '0',
        },
        {
            key: '2-1',
            id: '14',
            name: '优惠券列表',
            level: '二级',
            nameshow: '/coupon',
            showicon: '0',
            ifshow: true,
            sort: '0',
        },
        {
            key: '2-2',
            id: '16',
            name: '品牌推荐',
            level: '二级',
            nameshow: '/homeBrand',
            showicon: '0',
            ifshow: true,
            sort: '0',
        },
        {
            key: '2-3',
            id: '17',
            name: '新品推荐',
            level: '二级',
            nameshow: '/homeNew',
            showicon: '0',
            ifshow: true,
            sort: '0',
        },
        {
            key: '2-4',
            id: '18',
            name: '人气推荐',
            level: '二级',
            nameshow: '/homeHot',
            showicon: '0',
            ifshow: true,
            sort: '0',
        }
    ],
    '3': [
        {
            key: '3-0',
            id: '22',
            name: '用户列表',
            level: '二级',
            nameshow: '/users',
            showicon: '0',
            ifshow: true,
            sort: '0',
        },
        {
            key: '3-1',
            id: '23',
            name: '角色列表',
            level: '二级',
            nameshow: '/roles',
            showicon: '0',
            ifshow: true,
            sort: '0',
        },
        {
            key: '3-2',
            id: '24',
            name: '菜单列表',
            level: '二级',
            nameshow: '/menus',
            showicon: '0',
            ifshow: true,
            sort: '0',
        },
        {
            key: '3-3',
            id: '25',
            name: '资源列表',
            level: '二级',
            nameshow: '/resource',
            showicon: '0',
            ifshow: true,
            sort: '0',
        }
    ]
};

Mock.mock(/\/menu\/list/, 'get', () => {
    console.log('/menu/list接口拦截');
    return { code: 1, msg: '获取成功', 
                menuData: menuData,
                expandedRowData: expandedRowData };
  });

  
Mock.mock(/\/menu\/edit/, 'post', (options) => {
    console.log('/menu/edit接口拦截');
    const { body } = options;
    const index = menuData.findIndex((ele) => ele.id === body.id);
    menuData.splice(index, 1, JSON.parse(body));
  
    return { code: 1, msg: '修改成功', 
            menuData: menuData,
            expandedRowData: expandedRowData };
});
  
Mock.mock(RegExp(`/menu/get.*`), 'get', (options) => {
    console.log('/menu/get/:id接口拦截');
    const arr = options.url.split('/');
    const id = arr[arr.length - 1];
    const data = menuData.find((ele) => ele.id === id);
    return { code: 1, msg: '获取成功', result: data };
});

Mock.mock(RegExp(`/menu/delete.*`), 'delete', (options) => {
    const arr = options.url.split('/');
    const id = arr[arr.length - 1];
    const index = menuData.findIndex((ele) => ele.id === id);
    menuData.splice(index, 1);
    return { code: 1, msg: '删除成功' };
});